// Fill out your copyright notice in the Description page of Project Settings.


#include "SSurveillanceWindow.h"
#include "SlateOptMacros.h"
#include "SurveillanceSubWindow.h"
#include "Blueprint/UserWidget.h"
#include "Components/GridSlot.h"


BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SSurveillanceWindow::Construct(const FArguments& InArgs)
{
	pGridPanel = SNew(SGridPanel);

	int32 MIndex = 0;
	int32 NameIndex = 0;
	for (int32 Row = 0; Row < InArgs._CameraInitArgs.Row; ++Row)
	{
		for (int32 Column = 0; Column < InArgs._CameraInitArgs.Column; ++Column)
		{
			USurveillanceSubWindow* SubWnd = CastChecked<USurveillanceSubWindow>(CreateWidget(InArgs._CameraInitArgs.World, InArgs._CameraInitArgs.SubWndClass));
			SubWnd->Row = Row;
			SubWnd->Column = Column;
			SubWnd->Padding = InArgs._CameraInitArgs.Padding;
			SubWnd->Dele_OnZoomBtnClicked.BindRaw(this, &SSurveillanceWindow::OnZoomBtnClicked);
			if (MIndex < InArgs._CameraInitArgs.ImageMaterials.Num())
			{
				SubWnd->SetImageMaterial(InArgs._CameraInitArgs.ImageMaterials[MIndex++].Get());
			}
			if (NameIndex < InArgs._CameraInitArgs.CameraNames.Num())
			{
				SubWnd->SetCameraName(InArgs._CameraInitArgs.CameraNames[NameIndex++]);
			}
			
			pGridPanel->SetColumnFill(Column, 1.f);

			pGridPanel->AddSlot(Column, Row)
			.Padding(InArgs._CameraInitArgs.Padding)
			[
				SubWnd->TakeWidget()
			];
		}
		pGridPanel->SetRowFill(Row, 1.f);
	}


	AddSlot()
	.HAlign(HAlign_Fill)
	.VAlign(VAlign_Fill)
	[
		pGridPanel.ToSharedRef()
	];

	SetActiveWidgetIndex(0);
}

void SSurveillanceWindow::OnZoomBtnClicked(bool IsZoom, USurveillanceSubWindow* SubWnd)
{
	if (IsZoom)
	{
		TSharedRef<SWidget> SubWndWidget = SubWnd->TakeWidget();
		pGridPanel->RemoveSlot(SubWndWidget);
		
		AddSlot()
		.HAlign(HAlign_Fill)
		.VAlign(VAlign_Fill)
		[
			SubWndWidget
		];
		
		SetActiveWidgetIndex(1);
	}
	else
	{
		TSharedRef<SWidget> ClickedSubWnd = GetActiveWidget().ToSharedRef();
		RemoveSlot(ClickedSubWnd);
		
		pGridPanel->AddSlot(SubWnd->Column, SubWnd->Row)
			.Padding(SubWnd->Padding)
			[
				ClickedSubWnd
			];
		
		SetActiveWidgetIndex(0);
	}
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION
